package cn.huaqingcheng.tianshu.core.uc.model;

import cn.huaqingcheng.tianshu.common.core.JsonTool;
import cn.huaqingcheng.tianshu.common.core.model.AttributeMap;
import cn.huaqingcheng.tianshu.common.core.model.CommonConvert;
import lombok.Data;
import lombok.experimental.Accessors;

/**
 * 背景设置
 */
@Data
@Accessors(chain = true)
public class BackgroundSetting {

    /**
     * 背景类型
     * 可选值: "image", "video", "slide"
     */
    private String type;

    /**
     * 图片背景设置
     */
    private ImageBackgroundSetting image;

    /**
     * 视频背景设置
     */
    private VideoBackgroundSetting video;

    /**
     * 遮罩设置
     */
    private MaskSetting mask;

    /**
     * 特效设置
     */
    private BackgroundSpecialEffect specialEffects;


    public static BackgroundSetting of(AttributeMap<?> map) {
        return JsonTool.copy(map, BackgroundSetting.class);
    }

    public static BackgroundSetting newDefault() {
        return new BackgroundSetting()
                .setType("image")
                .setImage(ImageBackgroundSetting.newDefault())
                .setVideo(VideoBackgroundSetting.newDefault())
                .setMask(MaskSetting.newDefault())
                .setSpecialEffects(BackgroundSpecialEffect.newDefault());
    }

    public String toJson() {
        return JsonTool.toJson(this);
    }

    public AttributeMap<Object> toMap() {
        return CommonConvert.INSTANCE.toMap(toJson());
    }

    /**
     * 图片背景设置
     */
    @Data
    @Accessors(chain = true)
    public static class ImageBackgroundSetting {

        /**
         * 图片 URI
         */
        private String uri;

        public static ImageBackgroundSetting newDefault() {
            return new ImageBackgroundSetting()
                    .setUri("");
        }

    }

    /**
     * 视频背景设置
     */
    @Data
    @Accessors(chain = true)
    public static class VideoBackgroundSetting {

        /**
         * 视频 URI
         */
        private String uri;

        /**
         * 视频播放速度
         */
        private Double rate;

        /**
         * 顶部裁剪
         */
        private Integer topCrop;

        /**
         * 右侧裁剪
         */
        private Integer rightCrop;

        /**
         * 底部裁剪
         */
        private Integer bottomCrop;

        /**
         * 左侧裁剪
         */
        private Integer leftCrop;

        /**
         * 顶部和底部裁剪是否相等
         */
        private Boolean topAndBottomEqualCrop;

        /**
         * 左部和右侧裁剪是否相等
         */
        private Boolean leftAndRightEqualCrop;


        public static VideoBackgroundSetting newDefault() {
            return new VideoBackgroundSetting()
                    .setUri("")
                    .setRate(0.0D)
                    .setTopAndBottomEqualCrop(Boolean.TRUE)
                    .setLeftAndRightEqualCrop(Boolean.TRUE)
                    .setTopCrop(0)
                    .setRightCrop(0)
                    .setBottomCrop(0)
                    .setLeftCrop(0);
        }

    }

    /**
     * 遮罩设置
     */
    @Data
    @Accessors(chain = true)
    public static class MaskSetting {

        /**
         * 遮罩是否启用
         */
        private Boolean enable;

        /**
         * 遮罩颜色
         */
        private String color;

        /**
         * 遮罩透明度
         */
        private Double opacity;

        public static MaskSetting newDefault() {
            return new MaskSetting()
                    .setEnable(false)
                    .setColor("rgba(0, 0, 0, .5)")
                    .setOpacity(0.0D);
        }

    }

    @Data
    @Accessors(chain = true)
    public static class BackgroundSpecialEffect {

        /**
         * 模糊效果 单位像素
         */
        private Integer blur;

        public static BackgroundSpecialEffect newDefault() {
            BackgroundSpecialEffect effect = new BackgroundSpecialEffect();
            effect.setBlur(0);
            return effect;
        }

    }

}
